	
	global option    varlabels tex plain fragment bdec(3) se starloc(1) starlevels(10 5 1) noautosumm
	global option_N    varlabels tex plain fragment bdec(3) se starloc(1) starlevels(10 5 1) summstat(N)
	global option_av varlabels tex plain fragment bdec(3)  se nocenter   nostar  noautosumm
	global option_av_N varlabels tex plain fragment bdec(3) se nocenter nostar  summstat(N)
	

 cd  "/Users/eliemurard/Dropbox/Household dissolution and migrants' education/files for replication/"
 
 
 
**********************************************************************************	
*	TABLE 4
**********************************************************************************
	
	
	 use ENOE_1stinterview.dta,clear 
	 
	 
	
	 
	 * sample: non attriter-household 
	 keep if nonattriter==1 	
	 	
		
	cap drop conss
	g conss=1 
	
	tempvar samplee
	g `samplee'= 1 
	
	global varlist   grandparent_rel   grandparent_maternal grandparent_paternal 
	global num : list sizeof global(varlist)

	
	local j=1
	foreach y of varlist  $varlist conss    {
		
		local l: variable label `y'
		
		outreg,clear(b`j') 
		nois dis "`y'"
		if "`y'" =="conss"{
		global oav ${option_av_N} 
		global odif ${option_N}  
		}
		if "`y'" !="conss"{
		global oav ${option_av} 
		global odif ${option} 
		}

		cap drop x
		g x=  father_mig==0 
		label var x  "`l'"
		qui reg  `y' x   [aw=fac]    if x==1 & `samplee'==1  ,nocons 
		qui outreg, merge(b`j') nod ${oav}   keep(x) ctitle( "", "Father: Stayer")
		cap drop x
		g x=  father_mig==1 
		label var x  "`l'"
		qui reg  `y' x   [aw=fac]    if x==1 & `samplee'==1  ,nocons 
		qui outreg, merge(b`j')  nod   ${oav}   keep(x) ctitle( "", "Father: Migrant in the next 12 months ")
		cap drop x		
		g x= father_mig ==1   
		label var x  "`l'"
		qui reg  `y' x   [aw=fac]     if  `samplee'==1  
		qui outreg, merge(b`j')  nod   ${odif}   keep(x) ctitle( "", "(2)-(1) raw")
		cap drop x
		g x= father_mig ==1  
		label var x  "`l'"
		qui areg  `y' x    i.age i.male rural [aw=fac]  if  `samplee'==1, absorb(ent)  
		qui outreg, merge(b`j')   ${odif}   keep(x) ctitle( "", "(2)-(1) with controls")	

	local j=`j'+1
	}
	
	local a = 1+$num
	forvalues j=1(1)`a'{
		if `j'==1 {	
		qui  outreg , store(main)  replay(b`j')  
		}
		else{
		qui outreg , store(main) append(b`j') replay(main)  
		}
		local j=`j'+1
		}
	 
	 outreg , replace replay(main)  title("Table 4: Living arrangements for stayers and left behind before paternal migration")

	  

**********************************************************************************	
*	TABLE 5
**********************************************************************************
	
		
	
	use ENOE_all_interviews.dta,clear 
	
	
	global m1 rural  i.id_panelgroup_nent
	global m2 $m1   i.age   male   i.nbreschild_q1 i.nb_child5
	global m3 $m2   i.hhsize_q1 	i.age_mother i.yedu_mother i.age_father i.yedu_father 
	
	global model_1   "Dummies", "" \ "Rural, qs, State FE", "Yes" \ "Child’s age, sex and number of children", "No" \ "Household size, parents’ age and education", "No" 
	global model_2	 "Dummies", "" \ "Rural, qs, State FE", "Yes" \ "Child’s age, sex and number of children", "Yes" \ "Household size, parents’ age and education", "No" 
	global model_3	 "Dummies", "" \ "Rural, qs, State FE", "Yes" \ "Child’s age, sex and number of children", "Yes" \ "Household size, parents’ age and education", "Yes" 
	
	qui reg  attritN5    $m3 
	g eesample= e(sample)
	
	
****************************************
* Panel A: Household attrition 
****************************************
	
	global T1  father_mig_quartbef
	

foreach y of varlist  attritN5   {	

	local l  :  variable label `y'
	dis "`l'"
		
	tempvar samplee
			g `samplee'=  (n_ent==2 |n_ent==3|n_ent==4)   &  eesample==1  

	outreg,clear(main_A)

	foreach j in 1 2 3  {
	global mcontrol ${m`j'}
	qui su `y'      [aw=fac]  if   `samplee'==1 & father_mig_quartbef==0  
	local av: di %4.3f  r(mean)
			
		qui areg `y'    $T1  $mcontrol  [aw=fac]  if   `samplee'==1 ,  absorb(cve_ent)     vce(cluster id_h) 
		qui outreg, merge(main_A)   nod keep( $T1 ) ctitle("`l'", "")  ${option} addrows("", \"Average outcome stayers ", `av'  \ "", \  "${model_`j'}" ) 
		}
		}
	*
	nois outreg ,   replay(main_A)  ${option}    title("Table 5: Children moving out or grandparents moving in?") 
		
****************************************
* Panel B: Grandparents joining
****************************************	

	qui reg  Grand_New_after_qbef   $m3 
	g eesample2= e(sample)
	
local i=1
foreach y of varlist   Grand_New_after_qbef   {	

	local l  :  variable label `y'
	dis "`l'"
	tempvar samplee
	g `samplee'=  nonattriter ==1 &   inrange(n_ent,2,5) & eesample2==1    
	
	outreg, clear(new_dyna) 
		
	foreach j in 1 2 3  {
	
	global mcontrol ${m`j'}
	
	qui su `y'      [aw=fac]  if   `samplee'==1 & father_mig_quartbef==0  
	local av: di %4.3f  r(mean)
	
	qui areg `y'    $T1  $mcontrol  [aw=fac]  if   `samplee'==1 ,  absorb(cve_ent)   vce(cluster id_h)
	qui outreg, merge(new_dyna)   nod keep( $T1 ) ctitle("`l'", "`place'")  ${option} addrows("", \"average outcome stayers ", `av'  \ "", \  "${model_`j'}" ) 
		}
	local i = 1 + `i'
	}
	*
	nois outreg ,    replay(new_dyna)  ${option}    title("Table 5: Children moving out or grandparents moving in?") 

	
	
	
	
	
	
	
